import api from "../../api/label.js";

Page({
  data: {
    scanList: [],
    statusTypeList: {
      0: {
        statusName: '待核销',
        statusType: 'primary'
      },
      1: {
        statusName: '已核销',
        statusType: 'success'
      },
      2: {
        statusName: '核销失败',
        statusType: 'danger'
      },
      3: {
        statusName: '无需核销',
        statusType: 'default'
      }
    },
    labelCodes: [],
    pageSize: 10, //当前多少页
    pageNo: 0, //现在多少页
    total: 0, //最后一页
    cancelDate: '',
    showDialog: false,
    scanResultList: [],
    batchno: undefined,
    count: 0,
    custPoints: 0,
    salemanPoints: 0,
    isNoMore: false,
    triggered: true,
    disCheckBool: false,
    recordShow: false,
    cancelStatus: null
  },
  onLoad(e) {
    this.setData({
      batchno: Number(e.batchno),
    })
  },
  onShow() {
    this.getBatchno()
  },
  refresh() {
    this.getBatchno()
    this.setData({
      triggered: false
    })
  },
  //获取列表接口----page..接受实参
  getData(pageNo) {
    //接口获取
    api.cancelRecordDetailPage({
      pageNo: pageNo, //当前页
      pageSize: this.data.pageSize, //需要多少条数据
      batchno: this.data.batchno // 批次号
    }).then(res => {
      //判断接口返回是否有值
      if (res.code == 0) {
        //判断是否为第一页
        const list = res.data.list.map(ele => {
          return {
            checked: false,
            statusName: this.data.statusTypeList[ele.status].statusName,
            statusType: this.data.statusTypeList[ele.status].statusType,
            ...ele
          }
        })
        if (pageNo == 1) {
          //赋值
          this.setData({
            scanList: list, //列表数据
            total: res.data.total,
            pageNo
          })
        } else {
          //拼接数据concat
          this.setData({
            scanList: this.data.scanList.concat(list), //拼接列表数据
            isNoMore: list.length == 0,
            pageNo
          })
        }
      } else {
        //否则置空
        this.setData({
          scanList: [], //置空列表数据
        })
      }
      const disCheckBool = this.data.cancelStatus === 1
      this.setData({
        disCheckBool: disCheckBool,
        triggered: false
      })
    })
  },
  // 触底分页函数
  paging() {
    // 当前页 < 最后页
    if (this.data.pageNo < this.data.total) {
      // 调用列表函数pageNo+1
      this.getData(this.data.pageNo + 1)
    }
  },
  onChecked(e) {
    // const item = e.currentTarget.dataset.item
    // const index = e.currentTarget.dataset.index
    // this.data.scanList.forEach(ele => {
    //   if (ele.statusType === 0 && item.labelCode === ele.labelCode) {
    //     const bool = !ele.checked
    //     if (bool) {
    //       this.data.labelCodes?.push(item.labelCode)
    //     } else {
    //       const idx = this.data.labelCodes.findIndex(ele => ele === item.labelCode)
    //       this.data.labelCodes?.splice(idx, 1)
    //     }
    //     this.setData({
    //       ['scanList[' + index + '].checked']: bool,
    //     })
    //   }
    // })
  },
  async onCancelStatus() {
    wx.showToast({
      icon: 'loading',
      duration: 10000
    });
    try {
      wx.hideToast();
      const res = await api.cancelStatus({
        labelCodes: this.data.labelCodes,
        batchno: this.data.batchno // 批次号
      })
      console.log(res)
      if (res.code == 0) {
        this.setData({
          showDialog: true,
          scanResultList: res.data
        })
      } else {
        wx.showModal({
          title: '核销失败',
          content: res.msg,
          showCancel: false,
          confirmText: '我知道啦'
        })
      }
    } catch (err) {
      wx.showModal({
        title: '核销失败',
        content: res.msg,
        showCancel: false,
        confirmText: '我知道啦'
      })
    } finally {
      this.setData({
        labelCodes: []
      })
    }
  },
  async getBatchno() {
    const res = await api.cancelRecordPage({
      pageNo: 1, //当前页
      pageSize: 10, //需要多少条数据
      batchno: this.data.batchno // 批次号
    })
    this.getData(1); //默认传入第一页s
    if (res.data?.list?.length) {
      const data = res.data.list[0]
      this.setData({
        batchno: Number(data.batchno),
        cancelDate: Number(data.cancelDate || ''),
        count: Number(data.count || ''),
        custPoints: Number(data.custPoints || ''),
        salemanPoints: Number(data.salemanPoints || ''),
        cancelStatus: Number(data.cancelStatus),
      })
    }
  },
  onClose() {
    this.setData({
      showDialog: false
    });
    this.getBatchno();
  },
  onRecord() {
    wx.navigateTo({
      url: '/pages/record/index?batchno=' + this.data.batchno,
    })
    // this.setData({
    //   recordShow: true
    // })
  }
})